<?php
class recordmsgController extends AController{	
	private $msgtable='SMS';
	private $recordtable='TELRECORD';
	
	public function recordAction(){
		$page = $this->request->getGetParameter('p',1);
		$pagesize = $this->request->getGetParameter('ps',10);
		$sel['begtime'] = $bt = $this->request->getGetParameter('bt');
		$sel['endtime'] = $et = $this->request->getGetParameter('et');
		$sel['user_name'] = $name = $this->request->getGetParameter('name');
		$sel['tel_no'] = $phone = $this->request->getGetParameter('phone');	
		$sel['call_type'] = $way = $this->request->getGetParameter('way');
		if(isset($_POST['sub_record'])){
			$sel['begtime'] = $bt = $this->request->getPostParameter('bt');
			$sel['endtime'] = $et = $this->request->getPostParameter('et');
			$sel['user_name'] = $name = $this->request->getPostParameter('name');
			$sel['tel_no'] = $phone = $this->request->getPostParameter('phone');	
			$sel['call_type'] = $way = $this->request->getPostParameter('way');
		}
		$allCord = $this->getAllRecordInfo($page,$pagesize,$sel);
		$tolNum = $this->getRecordTolNum($sel);
		$this->allcord = $allCord;
		$this->page = $page;
		$this->pagesize = $pagesize;
		$this->bt = $bt;
		$this->et = $et;
		$this->name = $name;
		$this->phone = $phone;
		$this->way = $way;
		$this->tolNum = $tolNum[0]['NUM'];
		return $this->render();
	}
	public function msgAction(){
		$page = $this->request->getGetParameter('p',1);
		$pagesize = $this->request->getGetParameter('ps',10);
		$sel['begtime'] = $bt = $this->request->getGetParameter('bt');
		$sel['endtime'] = $et = $this->request->getGetParameter('et');
		$sel['oper_user'] = $name = $this->request->getGetParameter('name');
		$sel['mobile'] = $phone = $this->request->getGetParameter('phone');	
		if(isset($_POST['sub_msg'])){
			$sel['begtime'] = $bt = $this->request->getPostParameter('bt');
			$sel['endtime'] = $et = $this->request->getPostParameter('et');
			$sel['oper_user'] = $name = $this->request->getPostParameter('name');
			$sel['mobile'] = $phone = $this->request->getPostParameter('phone');	
		}
		$allMsg = $this->getAllMsgInfo($page,$pagesize,$sel);
		$tolNum = $this->getMsgTolNum($sel);
		$this->allmsg = $allMsg;
		$this->page = $page;
		$this->pagesize = $pagesize;
		$this->bt = $bt;
		$this->et = $et;
		$this->name = $name;
		$this->phone = $phone;
		$this->tolNum = $tolNum[0]['NUM'];
		return $this->render();
		return $this->render();
	}
	public function msg_watchAction(){
		$id = $this->request->getGetParameter('id');
		$msgInfo = $this->getMsgInfoById($id);
		$this->msginfo = $msgInfo;
		return $this->render();
	}
	public function record_playAction(){
		$id = $this->request->getGetParameter('id');
		$recodeInfo = $this->getRecodeInfoById($id);
		$this->rooturl = 'http://www.csmrecode.com';
		$this->recode = $recodeInfo;
		return $this->render();
	}
	function getAllRecordInfo($page,$pagesize,$select){
		
		if($this->unid<0){
			$where = '1=1';
		}else{
			$where = 'unit_id='.$this->unid;
		}
		if(count($select)>0){
			foreach ($select as $k=>$v){
				if(!empty($v)){
					if($k=='begtime'){
						$where .= ' and start_time>\''.$v.'\'';
						continue;
					}
					if($k=='endtime'){
						$where .= ' and start_time<\''.date('Y-m-d',strtotime($v)+3600*24).'\'';
						continue;
					}
					if($k=='user_name' || $k=='tel_no'){
						$where .= ' and '.$k.' like \'%'.$v.'%\'';
						continue;
					}
					$where .= ' and '.$k.'='.$v;
				}
			}
		}
//		echo $where;
		$order = 'start_time desc';
		return JYDB::findAll($this->recordtable,$where,'*',$order,$page,$pagesize);
	}
	function getAllMsgInfo($page,$pagesize,$select){
		if($this->unid<0){
			$where = '1=1';
		}else{
			$where = 'unit_id='.$this->unid;
		}
		if(count($select)>0){
			foreach ($select as $k=>$v){
				if(!empty($v)){
					if($k=='begtime'){
						$where .= ' and send_time>\''.$v.'\'';
						continue;
					}
					if($k=='endtime'){
						$where .= ' and send_time<\''.date('Y-m-d',strtotime($v)+3600*24).'\'';
						continue;
					}
					if($k=='oper_user' || $k=='mobile'){
						$where .= ' and '.$k.' like \'%'.$v.'%\'';
						continue;
					}
					$where .= ' and '.$k.'='.$v;
				}
			}
		}
//		echo $where;
		$order = 'send_time desc';
		return JYDB::findAll($this->msgtable,$where,'*',$order,$page,$pagesize);
	}
	function getRecordTolNum($select){
		if($this->unid<0){
			$where = '1=1';
		}else{
			$where = 'unit_id='.$this->unid;
		}
		if(count($select)>0){
			foreach ($select as $k=>$v){
				if(!empty($v)){
					if($k=='begtime'){
						$where .= ' and start_time>\''.$v.'\'';
						continue;
					}
					if($k=='endtime'){
						$where .= ' and start_time<\''.date('Y-m-d',strtotime($v)+3600*24).'\'';
						continue;
					}
					if($k=='user_name' || $k=='tel_no'){
						$where .= ' and '.$k.' like \'%'.$v.'%\'';
						continue;
					}
					$where .= ' and '.$k.'='.$v;
				}
			}
		}
//		echo $where;
		return JYDB::findAll($this->recordtable,$where,'count(*) num');
	}
	function getMsgTolNum($select){
		if($this->unid<0){
			$where = '1=1';
		}else{
			$where = 'unit_id='.$this->unid;
		}
		if(count($select)>0){
			foreach ($select as $k=>$v){
				if(!empty($v)){
					if($k=='begtime'){
						$where .= ' and send_time>\''.$v.'\'';
						continue;
					}
					if($k=='endtime'){
						$where .= ' and send_time<\''.$v.'\'';
						continue;
					}
					if($k=='oper_user' || $k=='mobile'){
						$where .= ' and '.$k.' like \'%'.$v.'%\'';
						continue;
					}
					$where .= ' and '.$k.'='.$v;
				}
			}
		}
//		echo $where;
		return JYDB::findAll($this->msgtable,$where,'count(*) num');
	}
	function getMsgInfoById($id){
		return JYDB::find($this->msgtable,'id='.$id);
	}
	function getRecodeInfoById($id){
		return JYDB::find($this->recordtable,'id='.$id);
	}
}
?>